<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * 用户相关的接口
 *
 * @author eoe2005
 */
class UserAction {
    /**
     *检查邮箱是否已将存在
     * @return type
     */
    public function checkMail(){
        $mail = post('mail');
        if($mail){
            if(isMail($mail)){
                $usermodel = getModel('User');
                if($usermodel->hasMail($mail)){
                    return Error::MAIL_IS_EXISTS;
                }else{
                    return array('not_exists' => 1);
                }
            }
            return Error::MAIL_IS_ERROR;
        }
        return Error::PARAM_ABSENCE;
    }
    /**
     *用户注册
     * @return type 
     */
    public function reg(){
        $mail = post('mail');
        $name = post('name');
        $pwd = post('pwd');
        $sex = intval(post('sex'));
        $birthday = intval(post('birthday'));
        $ip = post('ip');
        if(!$ip){
            $ip = getIP();
        }
        if($mail){
            if(isMail($mail)){
                if(empty($name)){
                    return Error::PARAM_ABSENCE;
                }elseif(empty($pwd)){
                    return Error::PARAM_ABSENCE;
                }
                $usermodel = getModel('User');
                if($usermodel->hasMail($mail)){
                    return Error::MAIL_IS_EXISTS;
                }    
                $uid = $usermodel->regUser($name,$mail,md5($pwd),$sex,$birthday,$ip);
                if($uid){
                    return array('uid' => $uid);
                }else{//这个是注册失败了
                    return Error::REG_FAIL;
                }
            }else{
                return Error::MAIL_IS_ERROR;
            }
        }else{
            return Error::PARAM_ABSENCE;
        }
        
    }
    
    /**
     *用户登录 
     */
    public function login(){
        $mail = post('mail');
        $pwd = post('pwd');
        $usermodel = getModel('User');
        $row = $usermodel->getUserByMail($mail);
        if($row){
            if($row['Fpwd'] == md5($pwd)){
                unset($row['Fpwd']);
                return $row;
            }
            return Error::UNAME_OR_PASSWD_IS_ERROR;
        }
        return Error::UNAME_OR_PASSWD_IS_ERROR;
    }
    /**
     *验证邮箱点击的连接
     * @return type 
     */
    public function verifyMail(){
        $uid = intval(post('uid'));
        $code = strval(post('code'));
        if($uid > 0 AND !empty($code)){
            $usermodel = getModel('User');
            $uinfo = $usermodel->getRegUser($uid);
            if($uinfo){
                if($uinfo['Fregtime'] < time() - 86400 * 3){
                    return Error::URL_TIMEOUT;
                }elseif($uinfo['Fcode'] !== $code){
                    return Error::URL_TIMEOUT;
                }else{
                    $newuid = $usermodel->verifyCode($uinfo);
                    if($newuid){
                        return $usermodel->getUserById($newuid);
                    }else{
                        return Error::OPTION_ERROR;
                    }
                }
            }else{
                return Error::USER_NOT_EXISTS;
            }
        }
        return Error::PARAM_ABSENCE;
    }
}

?>
